package cn.edu.scau.cmi.ema.domain;


import java.io.Serializable;

import java.lang.StringBuilder;

import java.math.BigDecimal;

import java.util.Calendar;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.persistence.*;
import javax.xml.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.*;
import java.util.Set;
import com.alibaba.fastjson.annotation.JSONField;

@Entity
@NamedQueries({
		@NamedQuery(name = "findAllPurchasematerials", query = "select myPurchasematerial from Purchasematerial myPurchasematerial"),
		@NamedQuery(name = "findPurchasematerialByName", query = "select myPurchasematerial from Purchasematerial myPurchasematerial where myPurchasematerial.name = ?1"),
		@NamedQuery(name = "findPurchasematerialByNameContaining", query = "select myPurchasematerial from Purchasematerial myPurchasematerial where myPurchasematerial.name like concat('%',?1,'%')"),
		@NamedQuery(name = "findPurchasematerialByDate", query = "select myPurchasematerial from Purchasematerial myPurchasematerial where myPurchasematerial.date = ?1"),
		@NamedQuery(name = "findPurchasematerialByPrice", query = "select myPurchasematerial from Purchasematerial myPurchasematerial where myPurchasematerial.price = ?1"),
		@NamedQuery(name = "findPurchasematerialByQuantity", query = "select myPurchasematerial from Purchasematerial myPurchasematerial where myPurchasematerial.quantity = ?1"),
		@NamedQuery(name = "findPurchasematerialByDescription", query = "select myPurchasematerial from Purchasematerial myPurchasematerial where myPurchasematerial.description = ?1"),
		@NamedQuery(name = "findPurchasematerialByDescriptionContaining", query = "select myPurchasematerial from Purchasematerial myPurchasematerial where myPurchasematerial.description like concat('%',?1,'%')"),
		@NamedQuery(name = "findAllPurchasematerialsByMaterial", query = "select myPurchasematerial from Purchasematerial myPurchasematerial where myPurchasematerial.material.id = ?1"),
		@NamedQuery(name = "findAllPurchasematerialsByProducer", query = "select myPurchasematerial from Purchasematerial myPurchasematerial where myPurchasematerial.producer.id = ?1"),
		@NamedQuery(name = "findAllPurchasematerialsBySupplier", query = "select myPurchasematerial from Purchasematerial myPurchasematerial where myPurchasematerial.supplier.id = ?1"),
		@NamedQuery(name = "findAllPurchasematerialsByUnit", query = "select myPurchasematerial from Purchasematerial myPurchasematerial where myPurchasematerial.unit.id = ?1"),
		@NamedQuery(name = "findPurchasematerialById", query = "select myPurchasematerial from Purchasematerial myPurchasematerial where myPurchasematerial.id = ?1"),
		@NamedQuery(name = "findPurchasematerialByPrimaryKey", query = "select myPurchasematerial from Purchasematerial myPurchasematerial where myPurchasematerial.id = ?1 "),
		@NamedQuery(name = "deletePurchasematerialsByIds", query = "delete Purchasematerial where id in (?1)"),
		@NamedQuery(name = "findPurchasematerialsByIds", query = "select myPurchasematerial from Purchasematerial myPurchasematerial where myPurchasematerial.id in (?1)"),
		@NamedQuery(name = "findPurchasematerialByPhyscialPrimaryKey", query = "select myPurchasematerial from Purchasematerial myPurchasematerial where  myPurchasematerial.producer.id = ?1 and myPurchasematerial.supplier.id = ?2 and myPurchasematerial.material.id = ?3 and myPurchasematerial.date = ?4"),
		@NamedQuery(name = "countPurchasematerialsByName", query = "select count(myPurchasematerial)  from Purchasematerial myPurchasematerial where myPurchasematerial.name like concat('%',?1,'%')") })
@Table(catalog = "ema", name = "purchasematerial")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "ema/cn/edu/scau/cmi/ema/domain", name = "Purchasematerial")
@XmlRootElement(namespace = "ema/cn/edu/scau/cmi/ema/domain")

public class Purchasematerial implements Serializable, Comparable<Purchasematerial> {

	private static final long serialVersionUID = 1L;

	@Column(name = "id", nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@XmlElement
	Integer id;

	@Column(name = "name")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String name;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "date")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	Calendar date;

	@Column(name = "price", precision = 22)
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	BigDecimal price;

	@Column(name = "quantity", precision = 22)
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	BigDecimal quantity;

	@Column(name = "description")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String description;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "material", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Material material;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "producer", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Producer producer;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "supplier", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Supplier supplier;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "unit", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Unit unit;

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return this.id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public void setDate(Calendar date) {
		this.date = date;
	}

	public Calendar getDate() {
		return this.date;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	public BigDecimal getPrice() {
		return this.price;
	}

	public void setQuantity(BigDecimal quantity) {
		this.quantity = quantity;
	}

	public BigDecimal getQuantity() {
		return this.quantity;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return this.description;
	}

	public void setMaterial(Material material) {
		this.material = material;
	}

	public Material getMaterial() {
		return material;
	}

	public void setProducer(Producer producer) {
		this.producer = producer;
	}

	public Producer getProducer() {
		return producer;
	}

	public void setSupplier(Supplier supplier) {
		this.supplier = supplier;
	}

	public Supplier getSupplier() {
		return supplier;
	}

	public void setUnit(Unit unit) {
		this.unit = unit;
	}

	public Unit getUnit() {
		return unit;
	}

	public void copy(Purchasematerial that) {

		setId(that.getId());

		setName(that.getName());
		setDate(that.getDate());
		setPrice(that.getPrice());
		setQuantity(that.getQuantity());
		setDescription(that.getDescription());

		setMaterial(that.getMaterial());

		setProducer(that.getProducer());

		setSupplier(that.getSupplier());

		setUnit(that.getUnit());

	}

	public Purchasematerial() {

	}

	@Override
	public String toString() {

		StringBuilder buffer = new StringBuilder();

		buffer.append("id=[").append(id).append("] ");

		buffer.append("name=[").append(name).append("] ");
		buffer.append("date=[").append(date).append("] ");
		buffer.append("price=[").append(price).append("] ");
		buffer.append("quantity=[").append(quantity).append("] ");
		buffer.append("description=[").append(description).append("] ");

		return buffer.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
		return result;
	}

	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (!(obj instanceof Purchasematerial))
			return false;
		Purchasematerial comparedPurchasematerial = (Purchasematerial) obj;
		if (!CheckPropertyEqual(this.producer.id, comparedPurchasematerial.producer.id)) {
			return false;
		}
		if (!CheckPropertyEqual(this.supplier.id, comparedPurchasematerial.supplier.id)) {
			return false;
		}
		if (!CheckPropertyEqual(this.material.id, comparedPurchasematerial.material.id)) {
			return false;
		}
		if (!CheckPropertyEqual(this.date, comparedPurchasematerial.date)) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}

	@Override
	public int compareTo(Purchasematerial o) {
		if (this.getId() > o.getId()) {
			return 1;
		} else if (this.getId() == o.getId()) {
			return 0;
		} else {
			return -1;
		}

	}
}
